package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.vo.CourserInfoVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.servicebase.excptionhandler.GuliException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author hjt
 * @since 2021-07-02
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    EduCourseService courseService;

    @Override
    public String addCourseInfo(CourserInfoVo courserInfoVo) {
        //向课程表里面添加数据
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courserInfoVo, eduCourse);
        boolean save = courseService.save(eduCourse);
//        int insert = baseMapper.insert(eduCourse);
        if (save == false) {
            throw new GuliException(20001, "添加课程信息失败");
        }
        String cid = eduCourse.getId();
        //向课程简介表添加数据
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courserInfoVo.getDescription());
        eduCourseDescription.setId(cid);
        eduCourseDescriptionService.save(eduCourseDescription);

        return cid;
    }
}
